package se.lnu.axez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.res.Resources;

/**
 * Utility class to access package resources.
 * 
 * @author stan
 *
 */
public final class ResourceUtility {
	
	/**
	 * This class cannot be instantiated.
	 */
	private ResourceUtility() {}
	
	/**
	 * Reads raw package resource as a string file.
	 * 
	 * @param resources
	 *            - resources to read string data from.
	 * @param id
	 *            - raw resource identifier.
	 * @return - String content of the raw resource file.
	 * @throws IOException
	 *             - is IO error occurs.
	 */
	public static String readStringResource(Resources resources, int id) throws IOException {
    	InputStreamReader isr = new InputStreamReader(resources.openRawResource(id));
    	BufferedReader br = new BufferedReader(isr);
    	
    	StringBuilder sb = new StringBuilder();
    	try {
	    	while (br.ready()) {
	    		sb.append(br.readLine()).append("\n");
	    	}
	    	
	    	return sb.toString();
		} finally {
    		try {
				br.close();
	    		isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
    }

}
